import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeSupport;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;

/**
 * JPanel that can be swapped out into the Display as an obstacle to the user
 */
public class Pirate extends State
{
    // public PropertyChangeSupport status;
    JPanel buttonPanel;

    public ItemBag shopItems;
    public BadThings badThings;

    /**
     * Create the panel.
     */
    public Pirate(Ship s)
    {
        super();
        theShip = s;
        badThings = new BadThings(theShip);
        // status = new PropertyChangeSupport(this);
        
        shopItems = new ItemBag();

        create();
    }

    /**
     * initialize the panel with proper buttons and spacing
     */
    public void create()
    {
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        setMaximumSize(new Dimension(800,700));

        
        JButton sailAround = new JButton("Sail Around!");
        sailAround.addActionListener(new sailAroundListener());
      
        JButton payUp = new JButton("Pay Up");
        payUp.addActionListener(new payUpListener());
        
        JButton charge = new JButton("Charge!");
        charge.addActionListener(new chargeListener());

        
        add(Box.createRigidArea(new Dimension(250,0)));
        add(sailAround);
        add(Box.createRigidArea(new Dimension(10,0)));
        add(payUp);
        add(Box.createRigidArea(new Dimension(10,0)));
        add(charge);
        
        
    }

    /**
     * paintComponent, puts in image and tells user what is happening
     */
    public void paintComponent(Graphics g)
    {
		
		super.paintComponent(g);
		
		try
        {
            g.drawImage(ImageIO.read(new File("piratebg.jpg")), 0, 0, null);
        }
        catch (IOException e)       {e.printStackTrace();}
        
        // draw text above buttons
        g.setFont(new Font("Papyrus", Font.ITALIC, 20));
        g.setColor(Color.white);
        g.drawString("Captain! We're being attacked by pirates,\n what should we do?!?!", 195, 40);
    }

   
    /**
     * 
     * class for sailing around, will add sailing states to ship's timeline.
     */
    private class sailAroundListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            status.firePropertyChange("sailAround", false, true);
        }
    }

    /**
     * 
     * detracts money from ship's bank and lets ship pass without adding states
     */
    private class payUpListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
        	int money = (int)(Math.random()*25) +15; //anywhere from 15-40 coins
           
                if (theShip.getInventory().consumeMoney(money)){
                	JOptionPane.showMessageDialog(buttonPanel, "The pirates took " + money + " from your savings!");
                    status.firePropertyChange("finished", false, true);

                }
                else
                {
                    JOptionPane.showMessageDialog(buttonPanel,
                            "Arrgggghhh matey, you are\nout of coin!",
                            "Plunder more!", JOptionPane.WARNING_MESSAGE);
                }

        }
    }

    /**
     * detracts from ship's health status or kills a crew member
     */
    private class chargeListener implements ActionListener
    {
    	public void actionPerformed(ActionEvent e){
    		Boolean result = badThings.pirateAttack();
    		if (!result)
    			status.firePropertyChange("PirateKill", false, true);
    		else
            status.firePropertyChange("finished", false, true);
        }
    }

    
    public String getType(){
    	return "Pirate";
    }
}